AddCSLuaFile("cl_init.lua")

AddCSLuaFile("shared.lua")

include("shared.lua")


function ENT:SpawnFunction(ply, tr) -- Spawn function needed to make it appear on the spawn menu
	if (!tr.HitWorld) then return end
 
	local ent = ents.Create("carGarage") -- Create the entity
	ent:SetPos(tr.HitPos + Vector(0, 0, 50)) -- Set it to spawn 50 units over the spot you aim at when spawning it
	ent:Spawn() -- Spawn it
 
	return ent -- You need to return the entity to make it work
end

function ENT:Initialize()
 
	self.Entity:SetModel("models/props_lab/blastdoor001c.mdl")
	self.Entity:PhysicsInit( SOLID_VPHYSICS )      -- Make us work with physics,
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )   -- after all, gmod is a physics
	self.Entity:SetSolid( SOLID_VPHYSICS )         -- Toolbox

	self.Entity:PhysicsInit(SOLID_VPHYSICS)

	local phys = self.Entity:GetPhysicsObject()

	if (phys:IsValid()) then
		phys:Wake()
	end
	 
	self.Entity:SetNetworkedBool("Usable", false)

	self.Entity:SetNetworkedBool("Plantable", true)

	//self.damage = 10

	local ply = self.Entity:GetNetworkedEntity("owning_ent")

	MakeNPCText(self.Entity, "Car Garage")

end

function ENT:Touch(hitEnt)
	local ply
	if hitEnt:IsVehicle() and self.Entity.LastTouch!=hitEnt then

		self.Entity.LastTouch = hitEnt
		timer.Simple(0.2, function() self.Entity.LastTouch = nil end)

		if hitEnt.VehicleTable and hitEnt.VehicleTable.Name and carSets[hitEnt.VehicleTable.Name] then
			if not hitEnt:GetDriver():GetTable().OwnedCars then hitEnt:GetDriver():GetTable().OwnedCars = {} end
			table.insert(hitEnt:GetDriver():GetTable().OwnedCars, hitEnt.VehicleTable.Name)
			saveOwnedCars(hitEnt:GetDriver())
			hitEnt:Remove()
		end
	end

end